package com.insurance.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.common.enums.DeleteFlagEnum;
import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.Query;
import com.insurance.dao.PartnerPermissionDao;
import com.insurance.entity.PartnerPermissionEntity;
import com.insurance.service.PartnerPermissionService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("partnerPermissionService")
public class PartnerPermissionServiceImpl extends ServiceImpl<PartnerPermissionDao, PartnerPermissionEntity> implements PartnerPermissionService {

    @Override
    public PageUtils queryPage(Map<String, Object> params, QueryWrapper<PartnerPermissionEntity> queryWrapper) {
        IPage<PartnerPermissionEntity> page = this.page(
                new Query<PartnerPermissionEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public QueryWrapper<PartnerPermissionEntity> getQueryWrapper(PartnerPermissionEntity partnerPermissionEntity){
        QueryWrapper<PartnerPermissionEntity> queryWrapper = new QueryWrapper<PartnerPermissionEntity>();
        queryWrapper.eq("delete_flag", String.valueOf(DeleteFlagEnum.UN_DELETE.getCode()));
        if(!StringUtils.isBlank(partnerPermissionEntity.getPermissionName())){
            queryWrapper.like("permission_name", partnerPermissionEntity.getPermissionName());
        }
        return queryWrapper;
    }

    /**
     * 获取查询条件
     *
     * @return
     */
    public QueryWrapper<PartnerPermissionEntity> getQueryWrapper(){
        QueryWrapper<PartnerPermissionEntity> queryWrapper = new QueryWrapper<PartnerPermissionEntity>();
        queryWrapper.eq("delete_flag", DeleteFlagEnum.UN_DELETE.getCode());
        return queryWrapper;
    }

    @Override
    public List<PartnerPermissionEntity> queryList(PartnerPermissionEntity partnerPermissionEntity){
        QueryWrapper<PartnerPermissionEntity> queryWrapper = getQueryWrapper(partnerPermissionEntity);
        return this.list(queryWrapper);
    }

    /**
     * 通过权限主键集合查询权限字符串集合
     *
     * @param permissionIdList 权限主键集合
     * @return
     */
    @Override
    public List<String> getPermissionByIds(List<String> permissionIdList){
        if(CollectionUtil.isEmpty(permissionIdList)){
            return new ArrayList<String>();
        }
        QueryWrapper<PartnerPermissionEntity> queryWrapper = getQueryWrapper();
        queryWrapper.in("permission_id", permissionIdList);
        List<PartnerPermissionEntity> partnerPermissionEntityList = this.list(queryWrapper);
        if(CollectionUtil.isEmpty(partnerPermissionEntityList)){
            return new ArrayList<String>();
        }
        List<String> permissionList = partnerPermissionEntityList.stream().map(PartnerPermissionEntity::getPermissionExpression).collect(Collectors.toList());
        return permissionList;
    }

    @Override
    public boolean removeBatch(List<String> permissionIdList, PartnerPermissionEntity partnerPermissionEntity){
        if(CollectionUtil.isEmpty(permissionIdList)){
            return true;
        }
        QueryWrapper<PartnerPermissionEntity> queryWrapper = new QueryWrapper<PartnerPermissionEntity>();
        queryWrapper.in("permission_id", permissionIdList);
        return this.update(partnerPermissionEntity,queryWrapper);
    }

}